/*
 * This file is part of quickReminder, a software for gamers
 *
 * Copyright (C) 2006-2008 Lucas Romero
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LucasCode;
using System.IO;
using System.Xml.Serialization;
using HtmlAgilityPack;
using System.Text.RegularExpressions;
using UserNotifier;

namespace ArchiveTester
{
    public partial class FormViewArchive : Form
    {
        WarnModule warner;
        MatchArchive archive;
        public FormViewArchive()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //archive.UpdateDetailsFromWeb();
            //RefreshView();
            
            int sl = 250;
            int ln = 500;
            List<HWBeepNote> ame = new List<HWBeepNote>();
            ame.Add(new HWBeepNote(HWBeepNotes.C, sl));
            ame.Add(new HWBeepNote(HWBeepNotes.D, sl));
            ame.Add(new HWBeepNote(HWBeepNotes.E, sl));
            ame.Add(new HWBeepNote(HWBeepNotes.F, sl));
            ame.Add(new HWBeepNote(HWBeepNotes.G, ln));
            ame.Add(new HWBeepNote(HWBeepNotes.G, ln));
            ame.Add(new HWBeepNote(HWBeepNotes.A, sl));
            ame.Add(new HWBeepNote(HWBeepNotes.A, sl));
            ame.Add(new HWBeepNote(HWBeepNotes.A, sl));
            ame.Add(new HWBeepNote(HWBeepNotes.A, sl));
            ame.Add(new HWBeepNote(HWBeepNotes.G, ln));
            ame.Add(new HWBeepNote(HWBeepNotes.Pause, sl));

            ame.Add(new HWBeepNote(HWBeepNotes.A, sl));
            ame.Add(new HWBeepNote(HWBeepNotes.A, sl));
            ame.Add(new HWBeepNote(HWBeepNotes.A, sl));
            ame.Add(new HWBeepNote(HWBeepNotes.A, sl));
            ame.Add(new HWBeepNote(HWBeepNotes.G, ln));
            ame.Add(new HWBeepNote(HWBeepNotes.Pause, sl));

            ame.Add(new HWBeepNote(HWBeepNotes.F, sl));
            ame.Add(new HWBeepNote(HWBeepNotes.F, sl));
            ame.Add(new HWBeepNote(HWBeepNotes.F, sl));
            ame.Add(new HWBeepNote(HWBeepNotes.F, sl));
            ame.Add(new HWBeepNote(HWBeepNotes.E, ln));
            ame.Add(new HWBeepNote(HWBeepNotes.E, ln));

            ame.Add(new HWBeepNote(HWBeepNotes.D, sl));
            ame.Add(new HWBeepNote(HWBeepNotes.D, sl));
            ame.Add(new HWBeepNote(HWBeepNotes.D, sl));
            ame.Add(new HWBeepNote(HWBeepNotes.D, sl));
            ame.Add(new HWBeepNote(HWBeepNotes.C, 1000));

            //warner = new HWKeyboardBlink(1000, KeyboardLEDs.CapsLock, 1000);
            //warner = new HWBeepWarnModule(10000, ame.ToArray());
            warner = new SoundWarnModule(5000, "sound.wav");
            //warner = new OSDWarnModule(1000);
            //warner = new ConsoleWarnModule(3000, 27006);
            warner.Test();
            //warner = new CDTrayWarnModule(2000,100);
            //warner = new HWBeepWarnModule(5000, 1000, 400);
            //warner = new GammeWarnModule(1000, 50);
            //warner.Start();
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (warner != null)
                warner.ActiveEvents = WarnEvents.None;


            return;

            MatchPlayer[] players = MatchContestant.getMemberList("2912083");
            MessageBox.Show(players.Length.ToString());
            foreach(MatchPlayer p in players)
            {
                if (p.IsBarred)
                {
                    string s = p.ESLNick + " is barred!" + Environment.NewLine;
                    
                    foreach(GameAccount g in p.GameAccounts)
                    {
                        s += g.Type + ": " + g.Value + Environment.NewLine;
                    }
                    MessageBox.Show(s);
                }
            }

            return;

            //RefreshView();
            HtmlAgilityPack.HtmlDocument d = new HtmlAgilityPack.HtmlDocument();
            d.Load("member.htm");
            d.OptionOutputAsXml = true;
            d.Save("esl.xml");
            //d.LoadHtml(HTML);
            //
            //return;

            //datetime + login-status

            HtmlNodeCollection members = d.DocumentNode.SelectNodes(@"//div[@id=""main_content""]/div/table/tbody/tr/td/table/tbody/tr/td/b/a");

            foreach (HtmlNode n in members)
            {
                //MessageBox.Show(n.WriteTo());
                HtmlNode GameIDNode = n.SelectSingleNode(@"../../../../../../../td[5]");
                foreach (HtmlNode GameID in GameIDNode.SelectNodes("div"))
                {
                    string[] gameaccount = GameID.InnerText.Split(new string[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);
                    string IDType = gameaccount[0].Trim();//Regex.Match(GameID.InnerText, @"^(.*?)\r\n").Groups[1].Value;
                    string id = gameaccount[1].Trim();
                    MessageBox.Show(IDType + " " + id);
                    
                    //if (steamid.Success)
                        //MessageBox.Show(IDType + " " + steamid.Groups[1].Value);
                }
                //MessageBox.Show(GameIDNode.InnerHtml);

            }

            /*HtmlNodeCollection results = d.DocumentNode.SelectNodes(@"//div[@id=""main_content""]/div/div/div[@class=""searchresult""]/a[@href]");
            foreach (HtmlNode n in results)
            {
                MessageBox.Show(n.InnerText + " (" + n.Attributes["href"].Value + ")");
            }*/

            //MessageBox.Show(MatchPlayer.lookupPlayer("0:0:27493").UserID);

            /*HtmlNodeCollection barredmembers = d.DocumentNode.SelectNodes(@"//div[@id=""main_content""]/div/div/table/tbody/tr");
            foreach (HtmlNode n in barredmembers)
            {
                MessageBox.Show(n.SelectSingleNode("td[2]").InnerText + " (" + n.SelectSingleNode("td[2]/a").Attributes["href"].Value + ")");
            }*/

            /*HtmlNode time = d.DocumentNode.SelectSingleNode(@"//div[@id=""datetime""]/div[2]");


            string ServerTimeString = time.SelectSingleNode("a[2]").InnerText.Trim();
            ServerTimeString = ServerTimeString.Remove(ServerTimeString.Length - 1);
            DateTime ServerTime = DateTime.Parse(ServerTimeString);

            bool isLoggedIn = time.SelectSingleNode("b/a").Attributes["href"].Value.Trim().EndsWith("/logout/");

            MessageBox.Show(ServerTime.ToString() + Environment.NewLine + isLoggedIn.ToString());

            HtmlNodeCollection matches = d.DocumentNode.SelectNodes(@"//div[@id=""main_content""]/div/table/tr[position()>=3]");
            
            foreach (HtmlNode n in matches)
            {
                string datestr = n.SelectSingleNode("td[1]").InnerText.Trim();
                //strip "Today, " or "Heute, " from string
                datestr = datestr.Substring(datestr.IndexOf(',') + 1, datestr.Length - datestr.IndexOf(',') - 1);
                DateTime date = DateTime.Parse(datestr);

                string team = Regex.Match(n.ParentNode.SelectSingleNode("tr[1]//span").InnerText.Trim(), @"^\( (.*?) \)$").Groups[1].Value; 

                string enemy = n.SelectSingleNode("td[2]").InnerText.Trim();
                string enemyurl = n.SelectSingleNode("td[2]//a[@href]").Attributes["href"].Value.Trim();
                string matchurl = n.SelectSingleNode("td[5]//a[@href]").Attributes["href"].Value.Trim();

                //string league = n.SelectSingleNode("td[5]/a").Attributes["href"].Value;

                MessageBox.Show(String.Format("Match vs. {0} on {1} found ({2})!\r\nTeam: {3}\r\nEnemy: {4} ({5})\r\nMatch: {6}", enemy, date, string.Empty, team, enemy, enemyurl, matchurl));
            }*/
        }

        private void button1_Click(object sender, EventArgs e)
        {
            GammaWarnModule.SetGamma((int)numericUpDown1.Value);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MatchArchive a = new MatchArchive("test.zip", true);
            MatchFile f = new MatchFile(@"C:\Dokumente und Einstellungen\Luigi\Desktop\roll16.png", "test.png");
            f.TargetName = "test.png";
            a.AddFile(f);
            a.FileProcess(f);
            //a.WriteArchiveDetails();
        }
    }
}